package com.lotusfall.mapper;

import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class HouseDynaSqlProviderBuilder {
    public String selectHouse(Map<String, Object> param) {
        SQL sql = new SQL() {
            {
                SELECT("*");
                FROM("tb_house_resources");
                if ((Integer)param.get("type") != -1 && (Integer)param.get("type") != 0) {
                    WHERE("house_type = #{type}");
                }
                if ((Integer)param.get("ori") != -1 && (Integer)param.get("ori") != 0) {
                    WHERE("orientation = #{ori}");
                }
                if ((Integer)param.get("area") != -1 && (Integer)param.get("area") != 0) {
                    if ((Integer)(param.get("area")) < 6) {
                        WHERE("covered_area BETWEEN (#{area}-1)*20 AND (#{area}*20)");
                    } else {
                        WHERE("covered_area > 100");
                    }

                }
                if ((Integer)param.get("rent") != -1 && (Integer)param.get("rent") != 0) {
                    if ((Integer)(param.get("rent")) < 2) {
                        WHERE("rent < 1000");
                    } else if (Integer.parseInt(param.get("area").toString()) < 8) {
                        WHERE("rent BETWEEN #{rent}*500 AND (#{rent} + 1)*500");
                    } else {
                        WHERE("rent > 100");
                    }

                }
                LIMIT("#{page},#{pageSize}");

            }
        };
        return sql.toString();
    }
}
